package com.city.parking;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.pingplusplus.android.PingppLog;
import com.pingplusplus.libone.PayActivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.support.v4.widget.DrawerLayout.DrawerListener;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class OrderDetailActivity extends FragmentActivity implements OnClickListener {
	private Button mPayBtn, mCancelBtn;
	private String URL = "http://120.25.153.123/park/getCharge";
	private String mName;
	private int mPrice;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_order_detail);
        mName = getIntent().getStringExtra("name");
        mPrice = getIntent().getIntExtra("price", 0);

		TextView titleTv = (TextView) findViewById(R.id.nav_title);
	    titleTv.setText("订单详情");

        ImageView back = (ImageView) findViewById(R.id.nav_ico_back);
        back.setOnClickListener(this);

        ImageView more = (ImageView) findViewById(R.id.nav_ico_more);
        more.setVisibility(View.GONE);

        mPayBtn = (Button) findViewById(R.id.pay_now);
        mPayBtn.setOnClickListener(this);
        mCancelBtn = (Button) findViewById(R.id.cancel_order);
        mCancelBtn.setOnClickListener(this);

        TextView name = (TextView) findViewById(R.id.parking_name);
        name.setText(mName);
        TextView price = (TextView) findViewById(R.id.order_value);
        price.setText(mPrice+"元");
        TextView payto = (TextView) findViewById(R.id.pay_to);
        payto.setText(mName);

		initPay();
	}

	private void initPay() {
		PayActivity.SHOW_CHANNEL_WECHAT = true;
		PayActivity.SHOW_CHANNEL_UPMP = true;
		PayActivity.SHOW_CHANNEL_BFB = true;
		PayActivity.SHOW_CHANNEL_ALIPAY = true;
		PingppLog.DEBUG = true;
	}

	private void startPay() {
		// 构建账单json对象
		JSONObject bill = new JSONObject();
		JSONObject displayItem = new JSONObject();
		try {
			displayItem.put("name", mName);
			bill.put("order_no", System.currentTimeMillis() + "");
			bill.put("amount", mPrice*100);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		// 发起支付
		PayActivity.CallPayActivity(this, bill.toString(), URL);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == PayActivity.PAYACTIVITY_REQUEST_CODE) {
			if (resultCode == PayActivity.PAYACTIVITY_RESULT_CODE) {
				Toast.makeText(this, data.getExtras().getString("result"),
						Toast.LENGTH_LONG).show();
			} else if (resultCode == Activity.RESULT_CANCELED) {
				// pressed back
				Toast.makeText(this, data.getExtras().getString("result"),
						Toast.LENGTH_LONG).show();
			}
		}
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.nav_ico_back:
			finish();
			break;
		case R.id.pay_now:
			startPay();
			break;
		case R.id.cancel_order:
			break;

		default:
			break;
		}
	}

}
